package com.learn.producer;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;

import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.concurrent.TimeoutException;

/**
 * @author DJJ_F
 * @version 1.0
 * @date 2025/08/15
 * @description ch01 hello——world 生产者
 */
public class Producer {
    public static void main(String[] args) throws IOException, TimeoutException {
        // 创建连接工厂，设置属性
        ConnectionFactory factory = new ConnectionFactory();
        factory.setHost("localhost");
        factory.setUsername("guest");
        factory.setPassword("guest");

        // 创建连接
        Connection connection = factory.newConnection();
        // 获取信道
        Channel channel = connection.createChannel();

        /*
        创建队列
        1. queue – the name of the queue
        2. durable – true 持久化队列
        3. exclusive – true 独占队列，只对应一个连接or消费者
        4. autoDelete – true 最后一个消费者放开链接以后，该队列自动删除
        5. arguments – other properties (construction arguments) for the queue
         */
        channel.queueDeclare("hello_world", false, false, false, null);

        String message = "Hello,world";

        /*
        发送消息，参数列表：
        1. exchange – 交换机
        2. routingKey – 路由的key值是哪个本次是队列的名称
        3. props – other properties for the message - routing headers etc
        4. body – the message body
         */
        channel.basicPublish("", "hello_world", null, message.getBytes(StandardCharsets.UTF_8));
        System.out.println("发送完毕---------------------");

    }
}
